package com.library.admin.listener;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import books.storage.service.AuthorDto;
import books.storage.service.BookDto;
import books.storage.service.LiteraryGenreDto;

import com.library.admin.AdmLibrary;
import com.library.admin.MainPanel;
import com.library.admin.form.AddBookForm;
import com.library.admin.list.BookTable;
import com.library.admin.manager.BookManagerPanel;
import com.library.admin.service.LibraryService;
import com.vaadin.data.Item;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Window.Notification;

public class AddBookListener implements ClickListener {
	private static final long serialVersionUID = -6385374733724930200L;

	private AddBookForm addBookForm;

	public AddBookListener(AddBookForm addBookForm) {
		this.addBookForm = addBookForm;
	}

	public void buttonClick(ClickEvent event) {
		AdmLibrary libraryApplication = (AdmLibrary) event.getComponent()
				.getApplication();
		if (addBookForm.isValid()) {
			MainPanel mainPanel = (MainPanel) libraryApplication.getMainPanel();
			BookTable bookTable = null;
			if (mainPanel.getComponentCount() > 0) {
				if (mainPanel.getComponent(0) instanceof BookManagerPanel) {
					BookManagerPanel bookManagerPanel = (BookManagerPanel) mainPanel
							.getComponent(0);
					bookTable = bookManagerPanel.getBookTable();

					try {
						// AJOUT D'UN LIVRE
						// ///////////////////
						int editionYear = Integer.valueOf(addBookForm
								.getBookEditionDateTextField().getValue()
								.toString().split("-")[0]);
						int editionMonth = Integer.valueOf(addBookForm
								.getBookEditionDateTextField().getValue()
								.toString().split("-")[1]);
						int editionDay = Integer.valueOf(addBookForm
								.getBookEditionDateTextField().getValue()
								.toString().split("-")[2]);
						GregorianCalendar _cal = new GregorianCalendar(
								editionYear, editionMonth, editionDay);
						XMLGregorianCalendar xmlCalendar = null;
						xmlCalendar = DatatypeFactory.newInstance()
								.newXMLGregorianCalendar(_cal);

						Object selectedAuthorId = addBookForm
								.getBookAuthorComboBox().getValue();
						Item selectedAuthorItem = addBookForm
								.getBookAuthorComboBox().getItem(
										selectedAuthorId);
						AuthorDto author = (AuthorDto) selectedAuthorItem
								.getItemProperty(AddBookForm.AUTHOR).getValue();

						Object selectedGenreId = addBookForm
								.getBookGenreComboBox().getValue();
						Item selectedGenreItem = addBookForm
								.getBookGenreComboBox()
								.getItem(selectedGenreId);
						LiteraryGenreDto genre = (LiteraryGenreDto) selectedGenreItem
								.getItemProperty(AddBookForm.GENRE).getValue();

						BookDto _newBook = new BookDto();
						_newBook.setAuthor(author);
						_newBook.setLiterary(genre);
						_newBook.setTitle(addBookForm.getBookTitleTextField()
								.getValue().toString());
						_newBook.setIsbn(addBookForm.getBookISBNTextField()
								.getValue().toString());
						_newBook.setEditionDate(xmlCalendar);

						BookDto _generateBk = LibraryService.getServiceBook()
								.addNewBook(_newBook);
						
						SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
						String editionDate = dateFormat.format(_newBook.getEditionDate().toGregorianCalendar().getTime());

						Button removeButton = new Button("X");
						String itemId = _newBook.getIsbn();
						removeButton.addListener(new RemoveBookListener(
								bookTable, itemId));
						
						bookTable.addItem(
								new Object[] { _newBook.getTitle(),
										_newBook.getAuthor().getName(),
										_newBook.getIsbn(),
										_newBook.getLiterary().getWording(),
										editionDate,
										_newBook.getCopiesNumber(),
										removeButton }, itemId);
						libraryApplication.getMainWindow().showNotification(
								"Le livre '" + _generateBk.getTitle()
										+ "' a été ajouté en base avec succés",
								Notification.TYPE_HUMANIZED_MESSAGE);
					}
					catch (Exception e) {
						libraryApplication.getMainWindow().showNotification(
								"Impossible d'ajouter ce livre",
								Notification.TYPE_ERROR_MESSAGE);
						e.printStackTrace();
					}
				}
			}
		}
		else {
			libraryApplication.getMainWindow().showNotification(
					"Veuillez remplir tous les champs obligatoires",
					Notification.TYPE_WARNING_MESSAGE);
		}
	}
}
